package com.atguigu.lease.common.minio;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class MinioConfiguration {
    @Autowired
    private MinioProperties minioProperties;

    public MinioClient getMinioClient(){
        MinioClient minioClient = MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
        return minioClient;
    }

    @Bean
    public MinioClient minioClient(){
        MinioClient minioClient = getMinioClient();
        try {
            boolean bucketExists = minioClient.bucketExists(BucketExistsArgs
                    .builder()
                    .bucket(minioProperties.getBucketName())
                    .build());
            if(!bucketExists){
                minioClient.makeBucket(MakeBucketArgs
                        .builder()
                        .bucket(minioProperties.getBucketName())
                        .build());

                String policy = """
                        {
                          "Statement" : [ {
                            "Action" : "s3:GetObject",
                            "Effect" : "Allow",
                            "Principal" : "*",
                            "Resource" : "arn:aws:s3:::%s/*"
                          } ],
                          "Version" : "2012-10-17"
                        }
                        """.formatted(minioProperties.getBucketName());

                minioClient.setBucketPolicy(SetBucketPolicyArgs
                        .builder()
                        .bucket(minioProperties.getBucketName())
                        .config(policy)
                        .build());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return minioClient;
    }
}
